package com.cgy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cgy.entity.Xibie;
import com.cgy.entity.ZiYuan;
import com.cgy.service.ZiYuanService;

import util.LayuiJsonUtils;

@RestController
@RequestMapping("ziyuan")
@CrossOrigin
public class ZiYuanController {
	@Autowired
	private ZiYuanService ziyuanService;
	@GetMapping
	public LayuiJsonUtils<ZiYuan> findAllByNameZiYuan(LayuiJsonUtils<ZiYuan> lj,ZiYuan z){
		Pageable pageable = PageRequest.of(lj.getPage(), lj.getLimit());
		Page<ZiYuan> xibie = ziyuanService.findAllByNameZiyuan(z, pageable);
		lj.setData(xibie.getContent());
		lj.setCount(xibie.getTotalElements());
		return lj;
	}
	@RequestMapping("findAllZiyuan")
	public List<ZiYuan> findAllz(){
		return ziyuanService.findAllZiYuan();
	}
	
	//添加机房
	@PostMapping
	public ZiYuan addZiYuan(ZiYuan z) {
		return ziyuanService.addAndUpdateZiYuan(z);
	}
	
	//修改机房
	@PutMapping
	public ZiYuan updateZiYuan(ZiYuan z) {
		return ziyuanService.addAndUpdateZiYuan(z);
	}
	
	//删除机房
	@DeleteMapping("{zid}")
	public Integer deleteZiYuan(@PathVariable("zid") Integer zid) {
		return ziyuanService.deleteZiyuan(zid); 
	}
}
